(function (factory) {
    var nameSpace = 'field_5550438035101852361';
    if (!window[nameSpace]) {
        var Builder = factory();
        window[nameSpace] = {
            instance: {}
        };
        window[nameSpace].init = function (options) {
            window[nameSpace].instance[options.privateId] = new Builder(options);
        };
        window[nameSpace].isNotNull = function (obj) {
            return true;
        };
    }
})(function () {
    /**
     * 构造函数
     *
     * @param options
     * @constructor
     */
    function App(options) {
        var self = this;
        // 初始化参数
        self.initParams(options);
        // 初始化dom
        self.initDom();
        // 事件
        self.events();
    }

    App.prototype = {
        initParams: function (options) {
            var self = this;
            self.adaptation = options.adaptation;
            self.privateId = options.privateId;
            self.messageObj = options.getData;
            self.preUrl = options.url_prefix;
        },
        initDom : function () {
            var self = this;
            self.appendChildDom();
        },
        events: function () {
            var self = this;
            // 监听是否数据刷新
            self.adaptation.ObserverEvent.listen('Event' + self.privateId, function () {
                self.messageObj = self.adaptation.childrenGetData(self.privateId);
                self.appendChildDom();
            });
        },
        appendChildDom: function () {
            var self = this;
            var showHTML = '';
            var display = self.messageObj.display.escapeHTML();
            var hasClick = true;
			
			if(display.indexOf("下拉多选")==0){
				showHTML += '<section class="cap4-people is-one "><div >';
				showHTML += '<div class="cap4-people__cnt" id="editDiv_'+self.privateId+'" style="display:block;">';
				if(self.messageObj.showValue ==''){
					showHTML += '<div class="cap4-people__browse" id="show_'+self.privateId+'"></div>'
				} else if(self.messageObj.value == self.messageObj.showValue){

                  var returnVal = $.ajax({url: '/seeyon/rest/cap4/qiangs/enumShowValue?flowNo='+self.messageObj.showValue,
	            		success: function (data) {
	            			var returnVal = data.data;
                            hasClick = false;
                            showHTML = returnVal;
                            if(self.messageObj.auth == 'edit'){
                                showHTML+='<div class="cap4-people__picker" id="click_'+self.privateId+'"><span class="ico24 related_document_24"></span></div>';
                                hasClick = true
                            }
                            document.querySelector('#editDiv_' + self.privateId).innerHTML = showHTML;
                            if(hasClick){
                                 document.querySelector('#click_'+ self.privateId).addEventListener('click', function() {
                                          self.location(self.privateId, self.messageObj, self.adaptation);
                                    });
                            }
	            		},
	                    error: function (e) {
	                    	$.alert("下拉多选表单控件:"+display+"的值展现异常!");
	                    }
            		});

				}else {
					showHTML += '<div class="cap4-people__browse" id="show_'+self.privateId+'">';
					showHTML += self.messageObj.showValue + '</div>'
				}
				showHTML += '<div class="cap4-people__picker" id="click_'+self.privateId+'">';
				//<img src="'+ window.top._ctxPath +'/apps_res/cap/multipleSelect/css/img/icon.png">
				// 编辑状态显示  选择图标
                if(self.messageObj.auth=='edit' || self.messageObj.auth==''){
                     showHTML += '<span class="ico24 related_document_24"></span>';
                }
				showHTML += '</div></div>';
				showHTML +="<div id='allmap'></div>";
				showHTML += "</div></section>";
			} else if(display.indexOf("修改按钮")==0){
				showHTML +=	'<div style="font-size:14px;height:24px;line-height:24px;cursor:pointer;background-color: #fff;border-radius: 15px;text-align: center;color: #1f85ec;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;border: 1px solid transparent;border-color: #1f85ec;"'
				+' id="click_'+self.privateId+'"><i class="icon CAP cap-icon-form-record" style="line-height:normal"></i>修改</div>'
			} else if(display.indexOf("Seep会议日程查看")==0){
				showHTML +=	'<div style="font-size:14px;height:24px;line-height:24px;cursor:pointer;background-color: #fff;border-radius: 15px;text-align: center;color: #1f85ec;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;border: 1px solid transparent;border-color: #1f85ec;"'
					+' id="click_'+self.privateId+'"><i class="icon CAP cap-icon-form-record" style="line-height:normal"></i>查看会议日程</div>';
            } else if(display.indexOf("生成企业报告")==0){
            	var data = window.thirdPartyFormAPI.getFormData();
            	hasClick = false;
				var flowNo = data.formmains[data.formData.tableInfo.formmain.tableName].field0229.value
            	var returnVal = $.ajax({url: '/seeyon/rest/cap4/qiangs/isReport?flowNo='+flowNo,
	            		success: function (data) {
	            			var returnVal = data.data;
	            			if(returnVal == "ok"){
	        					showHTML +=	'<div style="font-size:14px;height:24px;line-height:24px;background-color: #fff;border-radius: 15px;text-align: center;color: #1f85ec;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;border: 1px solid transparent;border-color: #1f85ec;filter: grayscale(1)"'
	        						+' id="click_'+self.privateId+'"><i class="icon CAP cap-icon-form-record" style="line-height:normal"></i>生成企业报告</div>';
	        				} else {
	        					showHTML +=	'<div style="font-size:14px;height:24px;line-height:24px;cursor:pointer;background-color: #fff;border-radius: 15px;text-align: center;color: #1f85ec;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;border: 1px solid transparent;border-color: #1f85ec;"'
	        						+' id="click_'+self.privateId+'"><i class="icon CAP cap-icon-form-record" style="line-height:normal"></i>生成企业报告</div>';
	        					hasClick = true;
	        				}
	            			document.querySelector('#' + self.privateId).innerHTML = showHTML;
	                        if(hasClick){
	            	            document.querySelector('#click_'+ self.privateId).addEventListener('click', function() {
	            	        		  self.location(self.privateId, self.messageObj, self.adaptation);
	            	    	  	});
	                        }
	            		},
	                    error: function (e) {
	                    	
	                    }
            		});
            }
            if(hasClick){
            	document.querySelector('#' + self.privateId).innerHTML = showHTML;
	            document.querySelector('#click_'+ self.privateId).addEventListener('click', function() {
	        		  self.location(self.privateId, self.messageObj, self.adaptation);
	    	  	});
            }
        },
        location: function (privateId, messageObj, adaptation) {
			var self = this;
			var tableName = self.messageObj.formdata.alldata.tableInfo.formmain.tableName;
			var fields = self.messageObj.formdata.formmains[tableName];
			var type = "";
			var filedConditionValue = "";
			
			if(messageObj.display.indexOf("下拉多选") == 0){
				var enumName = encodeURI(messageObj.display.split("_")[1]);
				var isTop = messageObj.display.split("_")[2];
				var dialog = $.dialog({
					id: 'dialog',
					url:  '/seeyon/qiangs.do?method=treePlug&selects='+messageObj.value+'&enumName='+enumName+"&showLevel="+isTop,  
					width: 450,
					height: 320,
					title: '选择多个选项',
					type : 'panel',
					transParams : {formmains :messageObj.formdata.formmains },
					checkMax:true,
					closeParam:{
						'show':false,
						autoClose:false,
						handler:function(){
						}
					},
					buttons: [{
						text: "确定",
						handler: function () {
							var address = dialog.getReturnValue();
							//messageObj = adaptation.childrenGetData(privateId);
							if("" == address.data){
								messageObj.showValue = "";
							} else {
								messageObj.showValue = address.show;
							}
							messageObj.value = address.data;
							messageObj.valueId = address.data;
							adaptation.childrenSetData(messageObj, privateId);
							dialog.close()
						}
					 }, {
						text: "取消",
						handler: function () {
							dialog.close()
						}
					}]
				});
			} else if(messageObj.display.indexOf("修改按钮") == 0){
				var data = window.thirdPartyFormAPI.getFormData();
				var toMasterDataId="";
				for(var i=0; i < data.formsons.front_formson_15.records.length; i++){
					if(data.formsons.front_formson_15.records[i].lists.field0462.recordId
						== messageObj.recordId){
						try{
							if('通过' == data.formsons.front_formson_15.records[i].lists.field0736.showValue){
								$.alert("审批已通过,不能修改!");
								return ;
							}
						} catch(e){
							if('通过' == data.formsons.front_formson_15.records[i].lists.field0919.showValue){
								$.alert("审批已通过,不能修改!");
								return ;
							}
						}
						toMasterDataId = data.formsons.front_formson_15.records[i].lists.field0217.relationData.toMasterDataId;
						break;
					}
				}
				$.ajax({url: '/seeyon/rest/cap4/qiangs/memberUpdateAuth',
            		success: function (data) {
            			var returnVal = data.data;
            			if(null == returnVal) {
							$.alert('不能获取编辑权限,请联系管理员处理.');
							return ;
						}
						openCtpWindow({
							id: 'dialog',
							url:  '/seeyon/cap4/businessTemplateController.do?method=formContent#/edit?rightId='
								+returnVal.split("|")[1]+'&moduleType=42&moduleId='+toMasterDataId
								+'&formTemplateId='+returnVal.split("|")[0]
						});
            		},
                    error: function (e) {
                    	$.alert('不能获取编辑权限,请联系管理员处理.');
                    }
        		});
			} else if(messageObj.display.indexOf("Seep会议日程查看") == 0){
				var data = window.thirdPartyFormAPI.getFormData();
				openCtpWindow({
					id: 'dialog',
					url:  '/seeyon/kkMeetingController.do?method=meetingReport&formmainId='
						+ data.formData.content.contentDataId+"&contentTemplateId="+data.formData.content.contentTemplateId
				});
			} else if(messageObj.display.indexOf("生成企业报告") == 0){
				var data = window.thirdPartyFormAPI.getFormData();
				var obj = new Object();
				obj.formmainId = data.formData.content.contentDataId+"";
				obj.formId = data.formData.content.contentTemplateId+"";
            	$.ajax({url: '/seeyon/rest/cap4/qiangs/generateReport?form='+obj.formId+'&&formmain='+obj.formmainId,
            		success: function (data) {
            			var returnVal = data.data;
            			if('ok' == returnVal){
							messageObj.value = returnVal;
							messageObj.valueId = returnVal;
							adaptation.childrenSetData(messageObj, privateId);
							$.alert("成功向启信宝发送生成企业报告请求!");
						} else {
							$.alert(returnVal);
						}
            		},
                    error: function (e) {
                    	$.alert("向启信宝发送生成企业报告请求异常,请联系管理员!");
                    }
        		});
			}
		}
    };
    
    return App;
});